package com.neu.af.dataFilter.service.impl;

import com.neu.af.dataFilter.entity.Rule;
import com.neu.af.dataFilter.entity.Tag;
import com.neu.af.dataFilter.entity.TagRuleAlarm;
import com.neu.af.dataFilter.mapper.RuleMapper;
import com.neu.af.dataFilter.mapper.TagMapper;
import com.neu.af.dataFilter.mapper.TagRuleAlarmMapper;
import com.neu.af.dataFilter.service.TagRuleAlarmService;
import com.neu.af.util.RegexUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("tagRuleAlarmService")
public class TagRuleAlarmServiceImpl implements TagRuleAlarmService {
    @Autowired
    TagRuleAlarmMapper tagRuleAlarmMapper;
    @Autowired
    TagMapper tagMapper;
    @Autowired
    RuleMapper ruleMapper;

    @Override
    public int insert(TagRuleAlarm tagRuleAlarm) {
        return tagRuleAlarmMapper.insert(tagRuleAlarm);
    }

    @Override
    public int update(TagRuleAlarm tagRuleAlarm) {
        return tagRuleAlarmMapper.update(tagRuleAlarm);
    }

    @Override
    public int delete(String id) {
        return tagRuleAlarmMapper.delete(id);
    }

    @Override
    public TagRuleAlarm getById(String id) {
        return tagRuleAlarmMapper.getById(id);
    }

    @Override
    public List<TagRuleAlarm> getAll() {
        return tagRuleAlarmMapper.getAll();
    }

    @Override
    public List<TagRuleAlarm> getAllByFilter(Map<String, Object> map) {
        return tagRuleAlarmMapper.getAllByFilter(map);
    }

    @Override
    public int deleteAll() {
        return tagRuleAlarmMapper.deleteAll();
    }

    @Override
    public void generateTagRuleAlarm() {
        tagRuleAlarmMapper.deleteAll();
        List<Tag> tags = tagMapper.getAll();
        List<Rule> rules_one = ruleMapper.getByAlarmClass("1");
        List<Rule> rules_two = ruleMapper.getByAlarmClass("2");
        for (Tag t: tags) {
            insertOne(t, rules_one, rules_two);
        }
    }

    @Override
    public void insertOne(Tag t, List<Rule> rules_one, List<Rule> rules_two) {
        TagRuleAlarm tagRuleAlarm = new TagRuleAlarm();
        String descriptor = t.getDescriptor();
        // 1. 如果包含 开启 或 复位，就啥也不干
        if (descriptor.contains("开启") || descriptor.contains("复位")) {
            return;
        }
        // 2. 判断是否以高或低或泄露结尾
        if (descriptor.endsWith("高") || descriptor.endsWith("低") || descriptor.endsWith("泄露")) {
            tagRuleAlarm.setDescriptor(descriptor);
            tagRuleAlarm.setTag_name(t.getTag_name());
            tagRuleAlarm.setAlarm_class(1);
            tagRuleAlarm.setRule_value("-1");
            if (descriptor.endsWith("高")) tagRuleAlarm.setContains("高");
            else if (descriptor.endsWith("低")) tagRuleAlarm.setContains("低");
            else if (descriptor.endsWith("泄露")) tagRuleAlarm.setContains("泄露");
            tagRuleAlarmMapper.insert(tagRuleAlarm);
            return;
        }
        // 3. 联锁和闭锁
        if (descriptor.contains("联锁") || descriptor.contains("闭锁")) {
            if( descriptor.contains("油位低联锁"))
            {
                tagRuleAlarm.setAlarm_class(1);
                tagRuleAlarm.setContains("油位低联锁");
            }
            else {
                tagRuleAlarm.setAlarm_class(2);
                if (descriptor.contains("联锁")) {
                    tagRuleAlarm.setContains("联锁");
                } else {
                    tagRuleAlarm.setContains("闭锁");
                }
            }
            tagRuleAlarm.setDescriptor(descriptor);
            tagRuleAlarm.setTag_name(t.getTag_name());
            tagRuleAlarm.setRule_value("-1");
            tagRuleAlarmMapper.insert(tagRuleAlarm);
            return;
        }

        boolean flag = false; // 记录一级列表是否找到
        // 4. 循环1级列表
        for (Rule r:rules_one) {
            String rule = r.getContains();
            // 匹配到
            if(descriptor.contains(rule)) {
                // 如果是阈值类
                if (rule.equals("大于") || rule.equals("小于") || rule.equals(">") || rule.equals("<") ){
                    String num = RegexUtil.getNum(rule, descriptor);
                    tagRuleAlarm.setRule_value(num);
                    // TODO: 2021/4/16 这边有大于/小于没值的情况，暂时插的空值
                }
                else {
                    tagRuleAlarm.setRule_value("-1");
                }
                tagRuleAlarm.setAlarm_class(1);
                tagRuleAlarm.setDescriptor(descriptor);
                tagRuleAlarm.setTag_name(t.getTag_name());
                tagRuleAlarm.setContains(rule);
                tagRuleAlarmMapper.insert(tagRuleAlarm);
                flag = true;
                return;
            }
        }
        // 没匹配到一级
        if (!flag) {
            // 含有AST和动作的特殊情况
            if (descriptor.contains("AST") && descriptor.contains("动作")) {
                tagRuleAlarm.setAlarm_class(1);
                tagRuleAlarm.setRule_value("-1");
                tagRuleAlarm.setDescriptor(descriptor);
                tagRuleAlarm.setTag_name(t.getTag_name());
                tagRuleAlarm.setContains("AST,动作");
                tagRuleAlarmMapper.insert(tagRuleAlarm);
                return;
            }

            for (Rule r:rules_two) {
                String rule = r.getContains();
                // 匹配到
                if(descriptor.contains(rule)) {
                    tagRuleAlarm.setAlarm_class(2);
                    tagRuleAlarm.setRule_value("-1");
                    tagRuleAlarm.setDescriptor(descriptor);
                    tagRuleAlarm.setTag_name(t.getTag_name());
                    tagRuleAlarm.setContains(rule);
                    tagRuleAlarmMapper.insert(tagRuleAlarm);
                    return;
                }
            }
        }
        // 全都没匹配上
        tagRuleAlarm.setDescriptor(descriptor);
        tagRuleAlarm.setTag_name(t.getTag_name());
        tagRuleAlarmMapper.insert(tagRuleAlarm);
    }

    @Override
    public TagRuleAlarm getByTagName(String tagName) {
        return tagRuleAlarmMapper.getByTagName(tagName);
    }
}
